import {messages} from "share/common";
import React from 'react'
import {Button, message, Select} from 'antd'
import bookingManagerService from 'containers/booking-management/booking-manager.service'
const Option = Select.Option;
import 'styles/booking-management/booking-manager.scss'
import config from "config";
import BookUtil from "./booking-util";
import Selector from 'components/selector'
import AdvanceTable from 'components/advanced-table';

class BookingManagerView extends React.Component {
  constructor(props) {
    super(props);
    this.options = BookUtil.getStatusType();
    this.selectorItem = {
      url: `${config.baseUrl}/api/authorization/policy/deploy/user/list/v2?type=ROLE_COMPANY_BOOK`,
      label: 'userName',
      key: 'userOID',
      offlineSearchMode: true,
    }
    this.params = {
      oldFunction: false,
      operationRecordType: '',
      bookerOID: ''
    }
    this.state = {
      loading: false,
      pagination: {
        total: 0,
        current: 1,
        page: 0,
        pageSize: 10,
      },
      targetBookerOID: '',
      params: {
        oldFunction: false,
        operationRecordType: '',
        bookerOID: ''
      },
      isOldFunction: true,
      data: [],
      searchForm: [
        {
          component: 'Select',
          id: 'operationRecordType',
          label: messages('supplier-2.key3')/*类型*/,
          children: this.options.map(item => <Option key={item.value}>{item.label}</Option>),
          parser: value => ({
            operationRecordType: value,
          }),
          props: {
            style: {width: '100%'},
          },
          tagParser: (value) => BookUtil.tagParseFormat(value, this.options),
        },
        {
          component: (
            <Selector
              selectorItem={this.selectorItem}
              showSearch={true}
            />
          ),
          id: 'bookerOID',
          label: messages('supplier-2.key7')/*订票专员*/,/*订票专员*/
          props: {
            placeholder: messages('supplier-2.key2')/*请输入temp2*/,
          },
        }
      ],
      columns: [
        {
          title: messages('supplier-2.key83')/*分配状态*/,
          key: "isDeliver",
          dataIndex: "isDeliver",
          render: isDeliver => isDeliver ? <span>{messages('supplier-2.key84')/*已分配*/}</span> :
            <span>{messages('supplier-2.key85')/*未分配*/}</span>
        },
        {
          title: messages('supplier-2.key4')/*更新状态*/,
          key: "hasHandled",
          dataIndex: "hasHandled",
          render: hasHandled => hasHandled ? <span>-</span> : <span>{messages('supplier-2.key5')/*有更新*/}</span>
        },
        {
          title: messages('common.applicant')/*申请人*/,
          key: "name",
          dataIndex: "name",
        },
        {
          title: messages('common.submit.date')/*提交日期*/,
          key: "lastModifiedDate",
          dataIndex: "lastModifiedDate",
          render: lastModifiedDate => lastModifiedDate.substr(0, 10)
        },
        {
          title: messages('supplier-2.key71')/*申请单号*/,
          key: "businessCode",
          dataIndex: "businessCode",
        },
        {
          title: messages('supplier-2.key3')/*类型*/,
          key: "type",
          dataIndex: "type",
          render: record => record === 1001 ?
            <span>{messages('supplier-2.key86')/*订票*/}</span> : record === 1003 ?
              <span>{messages('supplier-2.key20')/*退票*/}</span> :
              <span>{messages('supplier-2.key87')/*改签*/}</span>
        },
        {
          title: messages('supplier-2.key7')/*订票专员*/,
          key: "bookerName",
          dataIndex: "bookerName",
        },
        {
          title: messages('supplier-2.key6')/*已记录*/,
          key: "hasRecorded",
          dataIndex: "hasRecorded",
        }

      ],
    }
  }

  componentWillMount() {
    bookingManagerService.travelSuppliers().then(res => {
      let isHave = false;
      if (res.data && res.data['2001']) {
        res.data['2001'].map(item => {
          if (item.supplyId === 10051) {
            isHave = true;
          }
        })
        if(isHave){
          this.setState({
            isOldFunction: false,
          }, () => {
            this.tableRef.getList();
          })
        }else{
          this.tableRef.getList();
        }
      }
    }).catch(err => {
      message.error(err.response.data.message)
    })
  }

  createTableOption = () => {
    const {searchForm, columns} = this.state;
    return {
      title: messages('supplier-2.key82')/*全局查看*/,
      key: 'plane_ticket',
      service: this.advanceTableServer,
      index: false,
      onRef: this.advanceTableRef,
      rowKey: (record, index) => index,
      columns: columns,
      searchForm: searchForm,
      footer: (
        <Button
          type="primary"
          onClick={this.exportAllTickes}
        >
          {messages('supplier-2.key88')/*导出订票任务*/}
        </Button>
      ),
      bordered: true,
    }
  }

  advanceTableRef = (ref) => {
    this.tableRef = ref;
  }

  advanceTableServer = (page, size, searchValues) => {
    const {isOldFunction} = this.state;
    searchValues.oldFunction = isOldFunction;
    this.params = searchValues;
    return bookingManagerService.getTraveList(page, size, searchValues).then(res => {
      if(res.data){
        return {
          data: {
            rows: res.data,
            total: Number(res.headers['x-total-count']) ? Number(res.headers['x-total-count']) : 0,
          }
        }
      }
    });
  }

  // 导出所有订票任务
  exportAllTickes = () => {
    this.setState({
      loading: true,
    })
    bookingManagerService.exportAllData(this.params).then(res => {
      this.setState({
        loading: false,
      })
      if(res.data.status === 'S'){
        window.open(res.data.path);
      }else{
        message.error(res.data.errorDetail);
      }
    })
  };

  render() {
    return (
      <div className="booking-manager">
        <div style={{height: 'calc(100vh - 116px)'}}>
          <AdvanceTable {...this.createTableOption()} />
        </div>
      </div>
    )
  }
}


export default BookingManagerView;

